﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Utilities;
using System.IO;

namespace t_Utilities
{
    /// <summary>
    /// Summary description for t_FileIO
    /// </summary>
    [TestClass]
    public class t_FileIO
    {
        public t_FileIO()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void TestReadback()
        {
            FileInfo output = new FileInfo("junk.txt");
            using (TextWriter wr = output.CreateText())
            {
                wr.WriteLine("line 1");
                wr.WriteLine("line 2");
                wr.WriteLine("line 3");
                wr.Close();
            }

            int count = 0;
            foreach (var line in output.GetLineIterator())
            {
                Assert.IsTrue(count != 0 || line == "line 1", "line 1 not right");
                Assert.IsTrue(count != 1 || line == "line 2", "line 2 not right");
                Assert.IsTrue(count != 2 || line == "line 3", "line 3 not right");
                Assert.IsTrue(count <= 2, "Read past end of file!");
                count = count + 1;
            }
            Assert.IsTrue(count == 3, "Wrong number of lines read!");
        }

        [TestMethod]
        public void TestLINQReadback()
        {
            FileInfo output = new FileInfo("junk.txt");
            using (TextWriter wr = output.CreateText())
            {
                for (int i = 0; i < 10; i++)
                {
                    wr.WriteLine("hi");
                }
                wr.Close();
            }

            var all = from l in output.GetLineIterator()
                      select l;

            Assert.IsTrue(all.Count() == 10, "Not all line scame back!");

            var lines = output.GetLineIterator().Take(5);
            Assert.IsTrue(lines.Count() == 5, "Wrong number of line scame back!");
        }

        [TestMethod]
        public void TestCopyToTemp()
        {
            FileInfo output = new FileInfo("junk.txt");
            using (TextWriter wr = output.CreateText())
            {
                for (int i = 0; i < 10; i++)
                {
                    wr.WriteLine("hi");
                }
                wr.Close();
            }

            FileInfo temp = output.CopyToTempFile();
            var all = from l in temp.GetLineIterator()
                      where l == "hi"
                      select l;
            Assert.IsTrue(all.Count() == 10, "Wrong number of lines in temp file");
        }

        [TestMethod]
        public void TestCreateAppendForce()
        {
            /// Make sure the create append thing works correctly
            
            /// Test a simple file

            FileInfo info = new FileInfo("TestCreateAppendForce.txt");
            using (TextWriter wr = info.CreateAppendForce())
            {
                wr.WriteLine("hi");
                wr.Close();
            }
            Assert.IsTrue(info.GetFirstLine() == "hi", "Line in test file is not wright");
            Assert.IsTrue(info.GetLineIterator().Count() == 1, "Wrong number of lines in test file");

            using (TextWriter wr = info.CreateAppendForce())
            {
                wr.WriteLine("there");
                wr.Close();
            }
            Assert.IsTrue(info.GetFirstLine() == "hi", "Line in test file is not wright");
            Assert.IsTrue(info.GetLineIterator().Count() == 2, "Wrong number of lines in test file");

            /// Make sure subdir works ok.
            FileInfo info1 = new FileInfo("TestCreateAppendForce\\TestCreateAppendForce.txt");
            using (TextWriter wr = info1.CreateAppendForce())
            {
                wr.WriteLine("dude");
                wr.WriteLine("hi");
                wr.Close();
            }
            Assert.IsTrue(info1.GetFirstLine() == "dude", "Line in test file is not wright subdir");
            Assert.IsTrue(info1.GetLineIterator().Count() == 2, "Wrong number of lines in test file subdir");
        }

        [TestMethod]
        public void TestReplaceText()
        {
            FileInfo finfo = new FileInfo("test.txt");
            using (TextWriter wr = finfo.CreateText())
            {
                wr.WriteLine("hiiii");
                wr.WriteLine("hhhh");
                wr.WriteLine("hitherehithere");
                wr.Close();
            }

            finfo.ReplaceText("hi", "dude");

            finfo.Refresh();
            Assert.IsTrue(finfo.Exists, "Replaced file no longer exists!");

            string[] lines = finfo.GetLineIterator().ToArray();

            Assert.IsTrue(lines.Length == 3, "Wrong number of lines in replaced file");
            Assert.IsTrue(lines[0] == "dudeiii", "Line 1 has wrong replacement");
            Assert.IsTrue(lines[1] == "hhhh", "Line 2 was modified");
            Assert.IsTrue(lines[2] == "dudetheredudethere", "Line 3 didn't have both guys replaced!");
        }

        [TestMethod]
        public void TestAddInitialLine()
        {
            FileInfo finfo = new FileInfo("test.txt");
            using (TextWriter wr = finfo.CreateText())
            {
                wr.WriteLine("hiiii");
                wr.WriteLine("hhhh");
                wr.WriteLine("hitherehithere");
                wr.Close();
            }

            finfo.InsertAsFirstLine("dude");

            finfo.Refresh();
            Assert.IsTrue(finfo.Exists, "Inserted file no longer exists!");

            string[] lines = finfo.GetLineIterator().ToArray();

            Assert.IsTrue(lines.Length == 4, "Wrong number of lines in replaced file");
            Assert.IsTrue(lines[0] == "dude", "Line 0 has wrong");
            Assert.IsTrue(lines[1] == "hiiii", "Line 1 has wrong");
            Assert.IsTrue(lines[2] == "hhhh", "Line 2 was wrong");
            Assert.IsTrue(lines[3] == "hitherehithere", "Line 3 is wrong!");
        }
    }
}
